﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using Dashboard.Expense.Business;
using Infragistics.Win.UltraWinGrid;
using Infragistics.Win;

namespace Dashboard.Expense
{
    public partial class FormExpenseForm : Form
    {
        public FormExpenseForm()
        {
            InitializeComponent();
        }

        private void FormUnproductiveTime_Load(object sender, EventArgs e)
        {
            try
            {
                this.textBoxStaffNo.Text = CommonBusiness.GetCurrentLoginUser();
                this.SetDefaultDateFromTo();
                this.BindApplicationTypes();
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);                
            }
        }

        private void BindApplicationTypes()
        {
            Exp_ExpenseType eet = new Exp_ExpenseType();
            DataTable types = eet.QueryApplicationTypes();

            this.ultraComboTypes.Items.Clear();
            foreach (DataRow row in types.Rows)
            {
                this.ultraComboTypes.Items.Add(new ValueListItem(row["Code"].ToString(), row["Code"].ToString()));
            }
        }

        private void toolStripButtonQuery_Click(object sender, EventArgs e)
        {
            QueryData();
        }

        private void QueryData()
        {
            try
            {
                Exp_ExpenseForm eef = new Exp_ExpenseForm();

                DateTime fromDate = this.dateTimePickerFrom.Text == "" ? DateTime.MinValue : this.dateTimePickerFrom.Value;
                DateTime toDate = this.dateTimePickerTo.Text == "" ? DateTime.MaxValue : this.dateTimePickerTo.Value.AddDays(1);
                string types = this.ultraComboTypes.CheckedItems.Count == 0 ? "" : "'" + this.ultraComboTypes.CheckedItems.ToString("','") + "'";

                DataTable result = eef.QueryExpenseForm(this.textBoxStaffNo.Text, fromDate, toDate, types);
                this.ultraGridMain.DataSource = result;
                this.ultraGridMain.DataBind();

                if (result.Rows.Count == 0)
                {
                    MessageBox.Show(this, "Not  found expense report.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);                
            }
        }

        private void toolStripButtonCreate_Click(object sender, EventArgs e)
        {
            using (FormAddExpenseForm addForm = new FormAddExpenseForm())
            {
                addForm.ShowDialog(this);

                this.QueryData();
            }
        }

        private void toolStripButtonDelete_Click(object sender, EventArgs e)
        {
            this.ultraGridMain.UpdateData();

            List<string> selectedIds = new List<string>();
            foreach (UltraGridRow row in this.ultraGridMain.Rows)
            {
                if (Convert.ToBoolean(row.Cells["Check"].Value) == true)
                {
                    selectedIds.Add(Convert.ToString(row.Cells["FormNo"].Value));
                }
            }

            if (selectedIds.Count == 0)
            {
                MessageBox.Show(this, "No data selected.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (MessageBox.Show(this, "Are you sure to delete selected records?", "Confirm",
                    MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                try
                {
                    this.Cursor = Cursors.WaitCursor;

                    Exp_ExpenseForm eef = new Exp_ExpenseForm();
                    foreach (var formNo in selectedIds)
                    {
                        eef.DeleteExpenseForm(formNo);
                    }
                    
                    MessageBox.Show(this, "Delete successfully.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    QueryData();
                    this.Cursor = Cursors.Default;
                }
            }
        }

        private void ultraGridMain_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            // 自适应列宽
            e.Layout.AutoFitStyle = Infragistics.Win.UltraWinGrid.AutoFitStyle.None;
            // 是否允许调整列宽
            e.Layout.Override.AllowColSizing = AllowColSizing.Free;
            // 是否允许移动栏位顺序
            e.Layout.Override.AllowColMoving = AllowColMoving.NotAllowed;

            // 设置Grid的Split窗口个数，建议设置为1--不允许Split
            e.Layout.MaxColScrollRegions = 1;
            e.Layout.MaxRowScrollRegions = 1;

            // 冻结列
            e.Layout.UseFixedHeaders = false;

            // 排序
            e.Layout.Override.HeaderClickAction = Infragistics.Win.UltraWinGrid.HeaderClickAction.SortSingle;

            // 不允许删除
            e.Layout.Override.AllowDelete = DefaultableBoolean.False;

            // 滚动提示
            //e.Layout.Bands[0].ScrollTipField = "StaffNo";

            // 隐藏不需要显示的列
            e.Layout.Bands[0].Columns["ID"].Hidden = true;
            e.Layout.Bands[0].Columns["DateFrom"].Hidden = true;
            e.Layout.Bands[0].Columns["DateTo"].Hidden = true;

            for (int i = 0; i < e.Layout.Bands[0].Columns.Count; i++)
            {
                if (e.Layout.Bands[0].Columns[i].Key != "Check")
                {
                    e.Layout.Bands[0].Columns[i].CellActivation = Activation.NoEdit;
                }
                else
                {
                    e.Layout.Bands[0].Columns[i].CellActivation = Activation.AllowEdit;
                }
            }
        }

        private void toolStripButtonExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void SetDefaultDateFromTo()
        {
            var today = DateTime.Today.Date;
            this.dateTimePickerFrom.Value = today.AddDays(-7).Date;
        }

        private void ultraGridMain_ClickCellButton(object sender, CellEventArgs e)
        {
            if (e.Cell.Column.Key == "Edit")
            {
                using (FormAddExpenseForm addForm = new FormAddExpenseForm())
                {
                    addForm.ID = int.Parse(e.Cell.Row.Cells["ID"].Value.ToString());
                    addForm.FormNo = int.Parse(e.Cell.Row.Cells["FormNo"].Value.ToString());
                    addForm.StaffNo = e.Cell.Row.Cells["StaffNo"].Value.ToString();
                    addForm.ShowDialog(this);

                    this.QueryData();
                }
            }

            if (e.Cell.Column.Key == "Print")
            {
                using (FormPrintDialog printForm = new FormPrintDialog())
                {
                    printForm.ID = int.Parse(e.Cell.Row.Cells["ID"].Value.ToString());
                    printForm.FormNo = int.Parse(e.Cell.Row.Cells["FormNo"].Value.ToString());
                    printForm.ShowDialog(this);
                }
            }
        }

    }
}
